package org.fh.controller.fhoa;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.fh.controller.base.BaseController;
import org.fh.entity.Page;
import org.fh.entity.PageData;
import org.fh.service.fhoa.CustomerTypeService;
import org.fh.util.Jurisdiction;
import org.fh.util.ObjectExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据客户类型检索
 */
@Controller
@RequestMapping("/customerType")
public class CustomerTypeController extends BaseController {
    @Autowired
    private CustomerTypeService customerTypeService;

    @Autowired
    private HttpServletRequest request;


    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/list")
//    @RequiresPermissions("customerType:list")
    @ResponseBody
    public Object list(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();

        String USERNAME = Jurisdiction.getUsername();                    //用户名
        page.setPd(pd);
        List<PageData> varList = customerTypeService.list(page);
        map.put("varList", varList);
        map.put("page", page);
        map.put("USERNAME", USERNAME);
        map.put("result", errInfo);
        return map;
    }


    /**
     * 导出到excel
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/excel")
    @RequiresPermissions("toExcel")
    public ModelAndView exportExcel() throws Exception {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<String> titles = new ArrayList<String>();
        titles.add("打卡地点");    //1
        titles.add("经度");    //2
        titles.add("纬度");    //3
        titles.add("图片地址");    //4
        titles.add("姓名");    //5
        titles.add("创建时间");    //6
        dataMap.put("titles", titles);
        List<PageData> varOList = customerTypeService.listAll(pd);
        List<PageData> varList = new ArrayList<PageData>();
        for (int i = 0; i < varOList.size(); i++) {
            PageData vpd = new PageData();
            vpd.put("var1", varOList.get(i).getString("LOANTITLE"));        //1
            vpd.put("var2", varOList.get(i).getString("LONGITUDE"));        //2
            vpd.put("var3", varOList.get(i).getString("LATITUDE"));        //3
            vpd.put("var4", varOList.get(i).getString("PHOTOADDA"));        //4
            vpd.put("var7", varOList.get(i).getString("USERNAME"));        //5
            vpd.put("var8", varOList.get(i).getString("CTIME"));        //6
            varList.add(vpd);
        }
        dataMap.put("varList", varList);
        ObjectExcelView erv = new ObjectExcelView();
        mv = new ModelAndView(erv, dataMap);
        return mv;
    }

}
